package com.sojson.config.swagger;

import java.sql.Time;
import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.sojson.config.condition.Swagger2Condition;
import com.sojson.constant.ConstantByMap;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2配置(地址:接口路径+(/swagger-ui.html或者/doc.html))
 * 
 * @author liu
 * @date 2021-03-18
 */
@Configuration
/** 开启Swagger2 */
@EnableSwagger2
/** 是否注入这个Bean */
@Conditional(Swagger2Condition.class)
/** 如果需要认证功能就需要添加这个 */
@EnableSwaggerBootstrapUI
public class Swagger2Configurer {

    /** 项目标题 */
    @Value("${so.app.title}")
    private String title;
    /** 项目版本 */
    @Value("${so.app.version:1.0.0}")
    private String version;

    /**
     * 构建Api信息
     * 
     * @return
     */
    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        // 设置Title和Version
        docket = docket.apiInfo(apiInfo());
        // 设置扫描的包
        docket = docket.select().apis(RequestHandlerSelectors.basePackage(ConstantByMap.PACKAGE_CONTROLLER_ALL))
            .paths(PathSelectors.any()).build();
        // 设置将哪个对象转为哪个对象
        docket = docket.directModelSubstitute(Time.class, Date.class);
        docket = docket.directModelSubstitute(Integer.class, int.class);
        docket = docket.directModelSubstitute(Long.class, long.class);
        return docket;
    }

    /**
     * 构建Api接口信息
     * 
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title + "API").version(version).build();
    }

}